package com.ggs.rg.ybjb.systemmodule.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.pojomodule.util.constants.ErrorEnum;
import com.ggs.rg.ybjb.systemmodule.service.AdminService;
import com.ggs.rg.ybjb.systemmodule.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * ClassName:AdminController
 * Package:com.ggs.rg.ybjb.systemmodule.controller
 * Description:
 * 管理员控制类
 * @Date:2021/10/31 16:13
 * @Author:cxw
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    //添加公告
    @PostMapping("/tac/addTAC")
    public JSONObject addTAC(@RequestParam("text") String text, MultipartFile p1) throws IOException {
        return CommonUtil.successJson(adminService.addTAC(p1,text), ShiroUtils.getSession().getId().toString());
    }

    //删除公告
    @PostMapping("/tac/deleTAC")
    public JSONObject deleTAC(@RequestBody JSONObject jsonObject) {
        return CommonUtil.successJson(adminService.deleTAC(jsonObject),ShiroUtils.getSession().getId().toString());
    }

    //更新公告
    @PostMapping("/tac/updateTAC")
    public JSONObject updateTAC(@RequestParam Integer id,@RequestParam String text,MultipartFile p1) throws IOException {
        return CommonUtil.successJson(adminService.updateTAC(id,text,p1),ShiroUtils.getSession().getId().toString());
    }

    //获取全部公告
    @GetMapping("/tac/getTAC")
    public JSONObject getATC() {
        return CommonUtil.successJson(adminService.getATC(),ShiroUtils.getSession().getId().toString());
    }

    //获取全部订单
    @GetMapping("/order/getOrders")
    public JSONObject getOrders(@RequestParam(value = "pagesNumber",required = false) Integer pagesNumber) {
        if (StringTools.isNullOrEmpty(pagesNumber))
            pagesNumber = 1;
        else if (pagesNumber <= 0)
            pagesNumber = 1;
        return CommonUtil.successJson(adminService.getOrders(pagesNumber,-1),ShiroUtils.getSession().getId().toString());
    }

    //获取已付款订单
    @GetMapping("/order/getPaidOrders")
    public JSONObject getPaidOrders(@RequestParam(value = "pagesNumber",required = false) Integer pagesNumber) {
        if (StringTools.isNullOrEmpty(pagesNumber))
            pagesNumber = 1;
        else if (pagesNumber <= 0)
            pagesNumber = 1;
        return CommonUtil.successJson(adminService.getOrders(pagesNumber,1),ShiroUtils.getSession().getId().toString());
    }

    //获取未付款订单
    @GetMapping("/order/getUnpaidOrders")
    public JSONObject getUnpaidOrders(@RequestParam(value = "pagesNumber",required = false) Integer pagesNumber) {
        if (StringTools.isNullOrEmpty(pagesNumber))
            pagesNumber = 1;
        else if (pagesNumber <= 0)
            pagesNumber = 1;
        return CommonUtil.successJson(adminService.getOrders(pagesNumber,0),ShiroUtils.getSession().getId().toString());
    }

    //通过oid获取订单基本信息
    @GetMapping("/order/getOrderById")
    public JSONObject getUnpaidOrders(@RequestParam(value = "oid",required = false) Long oid) {
        if (StringTools.isNullOrEmpty(oid))
            return CommonUtil.errorJson(ErrorEnum.E_90003,ShiroUtils.getSession().getId().toString());
        return CommonUtil.successJson(adminService.getOrderById(oid),ShiroUtils.getSession().getId().toString());
    }

    //通过oid获取订单详细信息
    @GetMapping("/order/getOrderDetailById")
    public JSONObject getOrderDetailById(@RequestParam(value = "oid",required = false) Long oid) {
        if (StringTools.isNullOrEmpty(oid))
            return CommonUtil.errorJson(ErrorEnum.E_90003,ShiroUtils.getSession().getId().toString());
        return CommonUtil.successJson(adminService.getOrderDetailsById(oid),ShiroUtils.getSession().getId().toString());
    }

    //获取总订单数
    @GetMapping("/order/getOrderCount")
    public JSONObject getOrderCount(){
        return CommonUtil.successJson(adminService.getOrderCount(),ShiroUtils.getSession().getId().toString());
    }

    //获取交易总金额
    @GetMapping("/order/transactionAmount")
    public JSONObject transactionAmount(){
        return CommonUtil.successJson(adminService.transactionAmount(),ShiroUtils.getSession().getId().toString());
    }

    //获取所有用户的基本信息
    @GetMapping("/user/getUsers")
    public JSONObject getUsers(@RequestParam(value = "pagesNumber",required = false) Integer pagesNumber){
        if (StringTools.isNullOrEmpty(pagesNumber))
            pagesNumber = 1;
        else if (pagesNumber <= 0)
            pagesNumber = 1;
        return CommonUtil.successJson(adminService.getUsers(pagesNumber),ShiroUtils.getSession().getId().toString());
    }

    //获取系统总用户数
    @GetMapping("/user/getUserCount")
    public JSONObject getUserCount(){
        return CommonUtil.successJson(adminService.getUserCount(),ShiroUtils.getSession().getId().toString());
    }

    //根据手机号获取用户详细信息,包括用户订单信息,有多少订单给多少
    @GetMapping("/user/getUserDeatilById")
    public JSONObject getUserDeatilById(@RequestParam(required = false) String phone){
        if (StringTools.isNullOrEmpty(phone))
            return CommonUtil.errorJson(ErrorEnum.E_90003,ShiroUtils.getSession().getId().toString());
        return CommonUtil.successJson(adminService.getUserDeatilById(phone),ShiroUtils.getSession().getId().toString());
    }

    //添加商品
    //商品表内没有，添加商品表，再根据gid添加参数表
    @PostMapping(value="/goods/addGoods")
    public JSONObject AddGoods(String name,String gDesc,
                               Integer status,String type,
                               MultipartFile p1,String goodsList) throws IOException {
        System.out.println(name);
        System.out.println(gDesc);
        System.out.println(status);
        System.out.println(type);
        System.out.println(p1);
        System.out.println(goodsList);
        //判断参数是否为空
        if (StringTools.isNullOrEmpty(name) || StringTools.isNullOrEmpty(gDesc)
                || StringTools.isNullOrEmpty(p1) || StringTools.isNullOrEmpty(type)
                ||StringTools.isNullOrEmpty(status) || StringTools.isNullOrEmpty(goodsList)){
            return CommonUtil.errorJson(ErrorEnum.E_90003,ShiroUtils.getSession().getId().toString());
        }

        return CommonUtil.successJson(adminService.addGoods(name,gDesc,status,type,p1,goodsList),
                ShiroUtils.getSession().getId().toString());
    }

    @PostMapping("/goods/deleGoods")
    public JSONObject deleAllGoods(@RequestBody JSONObject jsonObject){
        Integer gid = jsonObject.getInteger("gid");
        return CommonUtil.successJson(adminService.deleteAllGoods(gid),ShiroUtils.getSession().getId().toString());
    }

    //删除商品某一个参数值
    @PostMapping("/goods/deleMater")
    public JSONObject deleGoodsMater(@RequestBody JSONObject jsonObject) {
        Integer pid = jsonObject.getInteger("pid");
        return CommonUtil.successJson(adminService.deleteGoodsMater(pid),ShiroUtils.getSession().getId().toString());
    }

    //查询商品 or 参数表关联的商品内容
    @GetMapping("/goods/getGoodsMater")
    public JSONObject getGoodsAndMater(@RequestParam Integer gid){
        return CommonUtil.successJson(adminService.selectGoodsAndMater(gid),ShiroUtils.getSession().getId().toString());
    }

    //模糊查询
    @PostMapping("/goods/getLike")
    public JSONObject getLike(@RequestBody JSONObject jsonObject){
        CommonUtil.hasAllRequired(jsonObject,"name,pagesNumber");
        String name = jsonObject.getString("name");
        Integer pagesNumber = jsonObject.getInteger("pagesNumber");
        return CommonUtil.successJson(adminService.selectLike(name,pagesNumber),ShiroUtils.getSession().getId().toString());
    }

    //获得全部商品与商品总数
    @GetMapping("/goods/getGoods")
    public JSONObject getGoods(@RequestParam(value = "pagesNumber",required = false) Integer pagesNumber){
        if (StringTools.isNullOrEmpty(pagesNumber)){
            pagesNumber = 1;
        }else if (pagesNumber <= 0){
            pagesNumber = 1;
        }
        return CommonUtil.successJson(adminService.selectGoods(pagesNumber),ShiroUtils.getSession().getId().toString());
    }

    //查询所有评论
    @GetMapping("/comment/getComments")
    public JSONObject getComments(@RequestBody JSONObject jsonObject){
        return CommonUtil.successJson(adminService.selectTComment(jsonObject),
                ShiroUtils.getSession().getId().toString());
    }

    //用户评论
    @PostMapping("/comment/addUserComment")
    public JSONObject addUserComment(MultipartFile p1,String text,Integer gid,String phone) throws IOException {
        if (StringTools.isNullOrEmpty(text) || StringTools.isNullOrEmpty(gid)
                || StringTools.isNullOrEmpty(p1) || StringTools.isNullOrEmpty(phone)){
            return CommonUtil.errorJson(ErrorEnum.E_90003,ShiroUtils.getSession().getId().toString());
        }
        return CommonUtil.successJson(adminService.addUserComment(p1,text,gid,phone),
                ShiroUtils.getSession().getId().toString());
    }

    //商家回复评论
    @PostMapping("/comment/addComment")
    public JSONObject addComment(@RequestBody JSONObject jsonObject){
        CommonUtil.hasAllRequired(jsonObject,"cid,textManage");
        Integer cid = jsonObject.getInteger("cid");
        String textManage = jsonObject.getString("textManage");
        return CommonUtil.successJson(adminService.addComment(cid,textManage),
                ShiroUtils.getSession().getId().toString());
    }

    //管理端首页数据展示
    //首页数据  获取前七天数据（订单数量、订单总金额）
    @GetMapping("/order/getOrderData")
    public JSONObject getAllOrder(){
        return CommonUtil.successJson(adminService.getOrderData(),
                ShiroUtils.getSession().getId().toString());
    }

    //添加库存
    @PostMapping("/goods/addCount")
    public JSONObject addCount(@RequestBody JSONObject jsonObject){
        CommonUtil.hasAllRequired(jsonObject,"cid,count");
        Integer cid = jsonObject.getInteger("cid");
        Integer count = jsonObject.getInteger("count");
        return CommonUtil.successJson(adminService.addCount(cid,count),
                ShiroUtils.getSession().getId().toString());
    }

    //查询缺货商品
    @GetMapping("/goods/selectBackorder")
    public JSONObject selectCount(@RequestParam(value = "pagesNumber",required = false) Integer pagesNumber){
        if (StringTools.isNullOrEmpty(pagesNumber)){
            pagesNumber = 1;
        }else if (pagesNumber <= 0){
            pagesNumber = 1;
        }
        return CommonUtil.successJson(adminService.selectBackorder(pagesNumber),
                ShiroUtils.getSession().getId().toString());
    }

    //模糊查询缺货商品
    @PostMapping("/goods/selectBackorderLike")
    public JSONObject selectBackorderLike(@RequestBody JSONObject jsonObject){
        String name = jsonObject.getString("name");
        Integer pagesNumber = jsonObject.getInteger("pagesNumber");
        if (StringTools.isNullOrEmpty(pagesNumber)){
            pagesNumber = 1;
        }else if (pagesNumber <= 0){
            pagesNumber = 1;
        }
        return CommonUtil.successJson(adminService.selectBackorderLike(name,pagesNumber),
                ShiroUtils.getSession().getId().toString());
    }





}
